/**
 * 自定义bpmn工具栏，代替默认的工具栏，完全自定义所有元素
 */
import type { PaletteEntry, PaletteProvider } from './PaletteType';

class CustomPaletteProvider implements PaletteProvider {
  private create: any;
  private elementFactory: any;
  // @ts-ignore
  private palette: any;

  static $inject = ['palette', 'create', 'elementFactory', 'globalConnect'];

  constructor(palette: any, create: any, elementFactory: any) {
    this.create = create;
    this.elementFactory = elementFactory;
    palette.registerProvider(this);
  }

  getPaletteEntries(): Record<string, PaletteEntry> {
    const { create, elementFactory } = this;

    function createAction(type: string, group: string, className: string, title: string): PaletteEntry {
      function createListener(event: Event): void {
        const shape = elementFactory.createShape({ type });
        create.start(event, shape);
      }

      return {
        group,
        className,
        title,
        action: {
          dragstart: createListener,
          click: createListener
        }
      }
    }

    return {
      'create.lindaidai-task': createAction(
        'bpmn:StartEvent',
        'model',
        'icon-custom lindaidai-task',
        '开始事件'
      ),
      "create.end-task": createAction(
        'bpmn:EndEvent',
        'model',
        'icon-custom lindaidai-task',
        '结束事件'
      ),
    }
  }
}

export default {
  __init__: ['paletteProvider'],
  paletteProvider: ['type', CustomPaletteProvider]
}